// components/phonevalidate/index.js
const app = getApp();
import common from "../../assets/common.js"
import api from "../../api/Api.js"
const { bidirectionalBind } = require('../../utils/bidirectionalBind.js');
import WxValidate from '../../utils/WxValidate.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    inputPhone: "",
    inputValidateCode: "",
    sendMsgValidateCodeButton: {
      text: "获取",
      disabled: false
    }
  },

  ready() {
    let _this = this;
    _this.initValidate();
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleInput(e) {
      bidirectionalBind(e, this);
    },
    /**
     * 发送验证码
     */
    sendMsgValidateCode() {
      let _this = this;
      let paras = {
        inputPhone: _this.data.inputPhone,
        inputValidateCode: "123456"
      }
      //值验证
      if (!this.WxValidate.checkForm(paras)) {
        const error = this.WxValidate.errorList[0];
        wx.showToast({
          title: error.msg,
          icon: 'none'
        });
        return;
      }
      let params = {
        phone: _this.data.inputPhone
      };
      let times = 60;
      _this.setData({
        'sendMsgValidateCodeButton.disabled': true
      });

      let inteval = setInterval(() => {
        _this.setData({
          'sendMsgValidateCodeButton.text': times + "s"
        });
        if (--times < 0) {
          _this.resetSendMsgValidateCodeButton();
          clearInterval(inteval);
        }
      }, 1000);
      api.user.sendMsgValidateCode(params).then(() => {
      }).catch(e => {
        _this.resetSendMsgValidateCodeButton();
        clearInterval(inteval);
        console.error(e);
      });
    },
    /**
     * 取消
     */
    cancel(e) {
      this.triggerEvent('cancel', e.detail);
    },
    /**
     * 确定
     */
    ok(e) {
      let _this = this;
      let params = {
        inputPhone: _this.data.inputPhone,
        inputValidateCode: _this.data.inputValidateCode,
      }
      //值验证
      if (!this.WxValidate.checkForm(params)) {
        const error = this.WxValidate.errorList[0];
        wx.showToast({
          title: error.msg,
          icon: 'none'
        });
        return;
      }
      let detail = e.detail ? e.detail : {};
      detail.phone = _this.data.inputPhone;
      detail.validateCode = _this.data.inputValidateCode;
      _this.triggerEvent('ok', detail);
    },
    resetSendMsgValidateCodeButton() {
      let _this = this;
      _this.setData({
        sendMsgValidateCodeButton: {
          text: "获取",
          disabled: false
        }
      });
    },
    /**
     * 初始化验证器
     */
    initValidate() {
      const rules = {
        inputPhone: {
          required: true,
          tel: true
        },
        inputValidateCode: {
          required: true,
          rangelength: [6, 6]
        }
      }
      const messages = {
        inputPhone: {
          required: '请输入手机号',
          tel: '请输入正确手机号'
        },
        inputValidateCode: {
          required: '请输入验证码',
          rangelength: '验证码为6位数'
        }
      }
      this.WxValidate = new WxValidate(rules, messages)
    },
  }
})
